package android.app.gfestival;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.gfestival.adapter.ImageLoader;
import android.app.gfestival.soap.About;
import android.app.gfestival.soap.SoapParserHelper;
import android.db.service.DatabaseService;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

public class AboutActivity extends Activity{
	private WebView mWebView;
	private DatabaseService service;
	private ProgressDialog progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about_layout);
		service = new DatabaseService(this);
		progress = showProgressDialog();
//		mWebView = (WebView) findViewById(R.id.webview);
//
//        WebSettings webSettings = mWebView.getSettings();
//        webSettings.setSavePassword(false);
//        webSettings.setSaveFormData(false);
//        webSettings.setJavaScriptEnabled(false);
//        webSettings.setSupportZoom(false);
//
////        mWebView.setWebChromeClient(new MyWebChromeClient());
//
////        mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
//        mWebView.setBackgroundColor(0);
//        mWebView.loadUrl("file:///android_asset/about.html");
		new AboutAsyntask().execute(null);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		service.onDestroy();
	}
	
	public ProgressDialog showProgressDialog() {
		ProgressDialog progress = new ProgressDialog(this.getParent(), R.style.ProgressDialogSpinner);
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setMessage("Loading...");
		progress.show();
		return progress;
	}
	
	class AboutAsyntask extends AsyncTask<String, String , About>{

		private ArrayList<About> list;

		@Override
		protected About doInBackground(String... params) {
			About about = null;
			try {
				if(service.isInternetConnected()){
					about = SoapParserHelper.getAbout().get(0);
					service.getAboutDAO().deleteAll();
					service.getAboutDAO().insert(about);
				}else{
					list = service.getAboutDAO().findAll();
					if(list.size() > 0){
						about = list.get(0);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return about;
		}
		
		@Override
		protected void onPostExecute(About result) {
			if(result != null){
				TextView title = (TextView) findViewById(R.id.about_title);
				title.setText(result.about_title);
				TextView desc = (TextView) findViewById(R.id.about_description);
				desc.setText(result.about_description);
				TextView info = (TextView) findViewById(R.id.about_practical_info);
				info.setText(result.about_practical_info);
				TextView contact = (TextView) findViewById(R.id.about_contact_info);
				contact.setText(result.about_contact_info);
				ImageView image = (ImageView) findViewById(R.id.imageview_about);
//				ImageLoader loader = new ImageLoader(AboutActivity.this);
//				loader.DisplayImage(result.about_picture, AboutActivity.this, image);
				WebView img = (WebView) findViewById(R.id.webview_about_img);
				img.loadUrl(result.about_picture);
			}
			progress.dismiss();
		}
		
	}
	
	/*
	 * 06-22 22:01:21.800: INFO/ABOUT(4881): -------->Struct{about_id=1; about_title=Kunning; 
	 * about_description=hetta er ein roynd 1234; 
	 * about_picture=http://www.g.halloapp.net/public/files/about_picture/GKunning_1308079091.jpg; 
	 * about_picture_directory=; about_practical_info=Taxa
	 * 
	 * 06-22 22:01:21.800: INFO/ABOUT(4881):                                                                                                                                                                                                 ; about_contact_info=this is not diplayed in .apk ?


	 */
}
